package cn.edu.dlut.ssdut.foodCredit2012.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.edu.dlut.ssdut.foodCredit2012.model.Category;
import cn.edu.dlut.ssdut.foodCredit2012.model.News;
import cn.edu.dlut.ssdut.foodCredit2012.model.TaxCollection;
import cn.edu.dlut.ssdut.foodCredit2012.model.User;
import cn.edu.dlut.ssdut.foodCredit2012.util.HibernateUtil;

public class CategoryService {

	public List<Category> getAllCagtegory() {
		List<Category> presentationList = null;
		Session session = HibernateUtil.getFileSessionFactory()
				.getCurrentSession();
		session.beginTransaction();

		Query query = session.createQuery("from Category");
		presentationList = (List<Category>) query.list();
		session.getTransaction().commit();
		return presentationList;
	}

	public Category getCategoryById(int id) {
		Session session = HibernateUtil.getFileSessionFactory()
				.getCurrentSession();
		session.beginTransaction();
		Category c = (Category) session.get(Category.class, id);
		session.getTransaction().commit();
		return c;
	}

	public Category getCategoryByNewsId(int id) {
		Session session = HibernateUtil.getFileSessionFactory()
				.getCurrentSession();
		session.beginTransaction();
		News n = (News) session.get(News.class, id);
		if(n!=null){
			Category cate=(Category)session.get(Category.class,n.getCid());
			return cate;
		}
		return null;
	}

}
